// pages/yaoKan/yaoKan.js
import {
  Get,
  Post,
  JsonPost
} from "../../utils/request.js"
let times 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginStatus: 0,
    moadelShow: false,
    heaImg: '',
    bargaining_id: null,
    self_id: null,
    info: {},
    goodsDetail: '',
    actionTab: 1,
    // 是否已砍
    ifKan:false,
    percent:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      bargaining_id: options.id,
      self_id: options.self_id,
      ifKan: options.ifKan=='true'?true:false,
    })
    this.getInfo()
    if (getApp().globalData.login) {
      this.setData({
        loginStatus: 1,
        heaImg: JSON.parse(wx.getStorageSync('userInfo')).avatarUrl
      })
    }
  },


  // 选择tab
  selectionTab(e) {
    this.setData({
      actionTab: e.currentTarget.dataset.index
    })
  },

  getInfo() {
    clearInterval(times)
    this.setData({
      info:{}
    })
    let that=this
    let data = {
      bargaining_id: this.data.bargaining_id,
      self_id: this.data.self_id,
    }
    JsonPost('bargaining/goodsbargainingdetails', data).then(res => {
      let obj=res.data[0]
      obj.addtime=((obj.addtime*1000)+(24 * 3600000))-new Date().getTime()
      let fieldDetail = res.data[0].goods_content || ''
      this.setData({
        info: obj,
        goodsDetail: fieldDetail.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display: block;margin: 20rpx auto;" ').replace(/<p>/ig, '<p class="p_class">'),
        percent:(Number(obj.goods_final_money)/Number(obj.goods_now_money))*100
      })
      let d = null;
      let h = null;
      let m = null;
      let s = null;
      let str = "info.addtime"
      that.setData({
        [str]: that.data.info.addtime - 1
      })
      let t = that.data.info.addtime;
      d = Math.floor(t / (24 * 3600));
      h = Math.floor((t - 24 * 3600 * d) / 3600);
      m = Math.floor((t - 24 * 3600 * d - h * 3600) / 60);
      s = Math.floor((t - 24 * 3600 * d - h * 3600 - m * 60));
      let dd = "info.dd"
      let hh = "info.hh"
      let mm = "info.mm"
      let ss = "info.ss"
      that.setData({
        [dd]: d,
        [hh]: h,
        [mm]: m,
        [ss]: s
      })
      if (that.data.info.addtime <= 0) {
        let str = "info.addtime"
        that.setData({
          [str]: 0
        })
        clearInterval(times)
      } else {
          times = setInterval(() => {
            let d = null;
            let h = null;
            let m = null;
            let s = null;
            let str = "info.addtime"
            that.setData({
              [str]: that.data.info.addtime - 1
            })
            let t = that.data.info.addtime;
            d = Math.floor(t / (24 * 3600));
            h = Math.floor((t - 24 * 3600 * d) / 3600);
            m = Math.floor((t - 24 * 3600 * d - h * 3600) / 60);
            s = Math.floor((t - 24 * 3600 * d - h * 3600 - m * 60));
            let dd = "info.dd"
            let hh = "info.hh"
            let mm = "info.mm"
            let ss = "info.ss"
            that.setData({
              [dd]: d,
              [hh]: h,
              [mm]: m,
              [ss]: s
            })
            if (that.data.info.addtime <= 0) {
              let str = "info.addtime"
              that.setData({
                [str]: 0
              })
              clearInterval(times)
            }
          }, 1000);
      }
    })
  },

  kanJ() {
    let data = {
      bargaining_id: this.data.bargaining_id,
      self_id: this.data.self_id,
    }
    JsonPost('bargaining/helpInitiate', data).then(res => {
      if (res.code == 0) {
        wx.showToast({
          title: '砍价成功',
          icon: 'none',
          duration: 2000
        })
        this.setData({
          moadelShow: true
        })
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },


  // 获取用户信息
  bindGetUserInfo(e) {
    getApp().ttClogin()
    getApp().userInfoReadyCallback = () => {
      this.setData({
        loginStatus: 1,
        heaImg: getApp().globalData.userInfo.avatarUrl
      })
    }
  },


  closMoadel() {
    this.setData({
      moadelShow: false
    })
  },

  goHome() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(times)
    this.setData({
      info:{}
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    if (options.from === 'button') {
      // 来自页面内转发按钮
      let shareObj = {
        title: '亲~帮忙砍个价 '+this.data.info.goods_name,
        path: "/pages/yaoKan/yaoKan?id=" + this.data.bargaining_id+'&self_id='+this.data.self_id,
        imageUrl: this.data.info.goods_img 
      }
      return shareObj;
    }
  }
})